-----------------------------------------------------------------------------
-- Faz escutar o som do dia(vento/musica) ou da noite(grilos)
-----------------------------------------------------------------------------

lunoturns = { }
lunoturns.players = { }
lunoturns.turno = nil
lunoturns.audios={ }
lunoturns.audios.dia = "bmg_background2"
lunoturns.audios.noite = "bmg_night"

if minetest.setting_getbool("lunomods_bgmusic") == nil or minetest.setting_getbool("lunomods_bgmusic") ~= false then
	minetest.setting_set("lunomods_bgmusic", "true")
	lunoturns.lunomods_bgmusic = true
else
	minetest.setting_set("lunomods_bgmusic", "false")
	lunoturns.lunomods_bgmusic = false
end


lunoturns.setBGM = function(player, audio)
	if player~=nil and player:is_player() ~= nil and player:is_player() and  
		lunoturns.players[player:get_player_name()] ~= nil 
	then
		if audio==nil or type(audio)~="string" or audio=="" then
			if lunoturns.players[player:get_player_name()].tocador ~= nil then
				minetest.sound_stop(lunoturns.players[player:get_player_name()].tocador)
			end
		else
			--minetest.after(1, function()
			--end)
			lunoturns.players[player:get_player_name()].tocador = minetest.sound_play(audio, {
				object = player, --Se retirar esta linha tocará para todos. (Provavelmente ¬¬)
				gain = 1.0, --Volume(1.0 = 100%)
				max_hear_distance = 1,
				loop = true,
			})
		end
	else
		print("[lunoturns.setBGM] #################### Falha de Listagem de player!!!" )
	end
	return
end

lunoturns.doTurno = function(player)
	if player~=nil and player:is_player() then
		if lunoturns.players[player:get_player_name()].mute==nil then 
			lunoturns.players[player:get_player_name()].mute=false 
		end
		if lunoturns.players[player:get_player_name()].mute==false then
			lunoturns.setBGM(player) --Isso da Stop na musica
			if lunoturns.turno == "dia" then 
				lunoturns.setBGM(player, lunoturns.audios.dia) 
			elseif lunoturns.turno == "noite" then 
				lunoturns.setBGM(player, lunoturns.audios.noite)
			end
		end
	else
		print("[lunoturns.doTurno] Falha de Listagem de player!!!" )
	end
end

lunoturns.calcTurno = function()
	if lunoturns.lunomods_bgmusic then
		local hora = minetest.get_timeofday() * 24
		--minetest.chat_send_all("hora(".. hora ..") Air("..pos.x..","..pos.y..","..pos.z..")")
		
		if hora>=5.5 and hora<=18.4 then -- O dia vai da 05h:30 as 18h:40
			turno = "dia"
		else
			turno = "noite"
		end
		
		if lunoturns.turno==nil or lunoturns.turno ~= turno then
			lunoturns.turno = turno
			for i,player in ipairs(minetest.get_connected_players()) do
				if player~=nil and player:is_player() then
					lunoturns.doTurno(player)
					
					--lunoturns.setTurno(player, turno)
				end
			end
		end
	end
	return
end

lunoturns.doMudo = function(name)
	if name ~= nil and name~="" then
		if lunoturns.players[name].mute==nil or lunoturns.players[name].mute==false then
			lunoturns.players[name].mute = true
			lunoturns.setBGM(minetest.get_player_by_name(name)) 
		else
			if lunoturns.lunomods_bgmusic then
				lunoturns.players[name].mute = false
				lunoturns.doTurno(minetest.get_player_by_name(name))
			end
		end
	end
end



minetest.register_abm({
	--nodenames = {"default:grass_1"},
	nodenames = {"default:dirt_with_grass"},
	--nodenames = {"air"},
	interval = 60.0, -- 10 segundo
	chance = 1, --1:10.000
	action = function(pos, node)
		lunoturns.calcTurno()
	end,
})

--[[
minetest.after(3, function()
	minetest.register_globalstep(function(dtime)
		lunoturns.calcTurno()
	end)
end)
]]--

minetest.register_on_joinplayer(function(player)
	if player ~= nil and player:is_player() then
		lunoturns.players[player:get_player_name()]={ }
		minetest.after(1, function()
			lunoturns.doTurno(player)
		end)
	end
end)

minetest.register_on_leaveplayer(function(player)
	if player ~= nil and player:is_player() then
		lunoturns.players[player:get_player_name()]=nil
	end
end)

minetest.register_chatcommand("mute", {
	params = "",
	description = "Ativa ou Desativa o audio de fundo.",
	privs = {},
	func = function(name, param)
		lunoturns.doMudo(name)
		return true
	end,
})

minetest.register_chatcommand("mudo", {
	params = "",
	description = "Ativa ou Desativa o audio de fundo.",
	privs = {},
	func = function(name, param)
		lunoturns.doMudo(name)
		return true
	end,
})


minetest.register_chatcommand("lunoturns", {
	params = "",
	description = "Exibe todos os comando deste mod",
	privs = {},
	func = function(name, param)
		minetest.chat_send_player(name, "    ", false)
		minetest.chat_send_player(name, "############################################################################################", false)
		minetest.chat_send_player(name, "### LUNOTURNS (TELA DE AJUDA DESTE MODING)                                               ###", false)
		minetest.chat_send_player(name, "### Desenvolvedor:'Lunovox Heavenfinder'<rui.gravata@gmail.com>                          ###", false)
		minetest.chat_send_player(name, "############################################################################################", false)
		minetest.chat_send_player(name, "FUNCAO DO MODDING:", false)
		minetest.chat_send_player(name, "   * Exibe um audio de fundo no cenario do jogador para identificar o turno do dia e", false)
		minetest.chat_send_player(name, "     noite.", false)
		minetest.chat_send_player(name, "    ", false)
		minetest.chat_send_player(name, "COMANDOS:", false)
		minetest.chat_send_player(name, "   * /mute", false)
		minetest.chat_send_player(name, "   * /mudo", false)
		minetest.chat_send_player(name, "       -> Ativa ou Desativa o audio de fundo.", false)
		minetest.chat_send_player(name, "############################################################################################", false)
		minetest.chat_send_player(name, name..", precione F10 e use a rolagem do mouse para ler todo este tutorial!!!", false)
	end,
})

print("[LUNOTURNS] Carregado com sucesso!!!" )

--FONTE:
--		https://forum.minetest.net/viewtopic.php?id=2897
--		http://dev.minetest.net/minetest.after

